#include "RoomLayer.h"
#include "../actions/CCallFuncDelay.h"
RoomLayer *RoomLayer::create()
{
    RoomLayer *pRet = new RoomLayer();
    if (pRet && pRet->init())
    {
        pRet->autorelease();
        return pRet;
    }
    else
    {
        CC_SAFE_DELETE(pRet);
        return NULL;
    }
}

bool RoomLayer::init()
{
	return IMILayer::init();
}

void RoomLayer::onEnter()
{
	IMILayer::onEnter();
	uiInit();
	setAllPosition();
	if(IMIData::getInstance()->isHaveCompiz)
	{
		
	}
}

void RoomLayer::onExit()
{
	IMILayer::onExit();
}

RoomLayer::~RoomLayer()
{
    unregisterScriptTouchHandler();
}

void RoomLayer::uiInit()
{
	m_bg=CCSprite::create("room/roombg.jpg");
	container->addChild(m_bg);
	m_talkBtn=IMITool::getInstance()->imiAddChild(container,"talkbtn.png");
	m_moreBtn=IMITool::getInstance()->imiAddChild(container,"roommore.png");

	nameIcon=IMITool::getInstance()->imiAddChild(container,"profileicon.png");
	moneytIcon=IMITool::getInstance()->imiAddChild(container,"moneyicon.png");	

	m_nameTxt=CCTextFieldTTF::textFieldWithPlaceHolder("loading",CCSize(100,50),kCCTextAlignmentLeft,"Arail",18);
	m_nameTxt->setColor(ccc3(227,182,135));
	m_nameTxt->setString("imihuang");
	container->addChild(m_nameTxt);

	m_moneyTxt=CCTextFieldTTF::textFieldWithPlaceHolder("loading",CCSize(100,50),kCCTextAlignmentLeft,"Arail",18);
	m_moneyTxt->setColor(ccc3(227,182,135));
	m_moneyTxt->setString("8,888");
	container->addChild(m_moneyTxt);

	m_headBg=IMITool::getInstance()->addChild(container,"bluebg.png");
	m_selfTime=CCLabelBMFont::create("", "room/timefnt.fnt"); 
	container->addChild(m_selfTime);
	m_selfTime->setVisible(false);
	setHeadTime(15);
}

void RoomLayer::setAllPosition()
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	m_bg->setAnchorPoint(ccp(0,1));
	m_bg->setPosition(ccp(0,IMIData::getInstance()->getOppsiteXY(0)));
	m_talkBtn->setAnchorPoint(ccp(0,1));
	m_talkBtn->setPosition(ccp(282,IMIData::getInstance()->getOppsiteXY(714)));
	m_moreBtn->setAnchorPoint(ccp(0,1));
	m_moreBtn->setPosition(ccp(382,IMIData::getInstance()->getOppsiteXY(714)));
	nameIcon->setAnchorPoint(ccp(0,1));
	nameIcon->setPosition(ccp(110,IMIData::getInstance()->getOppsiteXY(720)));
	moneytIcon->setAnchorPoint(ccp(0,1));
	moneytIcon->setPosition(ccp(110,IMIData::getInstance()->getOppsiteXY(760)));
	m_nameTxt->setAnchorPoint(ccp(0,1));
	m_nameTxt->setPosition(ccp(110+40,IMIData::getInstance()->getOppsiteXY(720)));
	m_moneyTxt->setAnchorPoint(ccp(0,1));
	m_moneyTxt->setPosition(ccp(110+40,IMIData::getInstance()->getOppsiteXY(760)));
	m_headBg->setAnchorPoint(ccp(0,1));
	m_headBg->setPosition(ccp(10,IMIData::getInstance()->getOppsiteXY(712)));
	m_selfTime->setAnchorPoint(ccp(0,1));
	m_selfTime->setPosition(ccp(18,IMIData::getInstance()->getOppsiteXY(732)));
}

void RoomLayer::setHeadColor(int type)
{
	CCSpriteFrame * temp=NULL;
	switch (type)
	{
	case 1:
		temp=IMITool::getInstance()->addChild(container,"redbg.png",false,false)->displayFrame();
		break;
	case 2:
		temp=IMITool::getInstance()->addChild(container,"ysbg.png",false,false)->displayFrame();
		break;
	case 3:
		temp=IMITool::getInstance()->addChild(container,"greenbg.png",false,false)->displayFrame();
		break;
	case 4:
		temp=IMITool::getInstance()->addChild(container,"bluebg.png",false,false)->displayFrame();
		break;
	default:
		break;
	}

	if(temp)
	{
		m_headBg->setDisplayFrame(temp);
	}
}

void RoomLayer::setHeadTime(int time)
{
	m_headTime=time;
	if(m_headTime<=0) return;
	m_selfTime->setVisible(true);
	
	CCallFuncDelay * action=CCallFuncDelay::create(1.0f,callfuncO_selector(RoomLayer::selfTimeHandler));
	this->runAction(action);
}

void RoomLayer::selfTimeHandler(CCObject * obj)
{
	m_selfTime->setString(IMITool::getInstance()->intToString(m_headTime).c_str());
	if(m_headTime>=10)
	{
		m_selfTime->setPositionX(18);
	}else
	{
		m_selfTime->setPositionX(37);
	}

	m_headTime--;
	if(m_headTime>=0)
	{
		CCallFuncDelay * action=CCallFuncDelay::create(1.0f,callfuncO_selector(RoomLayer::selfTimeHandler));
		this->runAction(action);
	}else
	{
		m_selfTime->setVisible(false);
	}
}


